package com.jianhong.li.test;

import com.jianhong.li.extend.SimpleDiff;
import com.jianhong.li.test.ObjectDiffTest.Bar;
import com.jianhong.li.test.ObjectDiffTest.MainObject;
import com.tujia.framework.diff.danielbechler.diff.ObjectDifferBuilder;
import com.tujia.framework.diff.danielbechler.diff.identity.IdentityStrategy;
import com.tujia.framework.diff.danielbechler.diff.node.DiffNode;
import com.tujia.framework.diff.danielbechler.diff.node.Visit;
import com.tujia.framework.diff.render.SimpleDiffPathFormatter;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;

/**
 * @author jianhong.li Date: 2019-10-16 Time: 3:23 PM
 * @version $Id$
 */
public class TujiaObjectDiff {
    @Test
    public void testSubListDiff() {
        final MainObject base = new MainObject();
        base.setName("测试主对象名称1");
        List<Bar> bars = new ArrayList<Bar>(1);
        Bar bar = new Bar();
        bar.setCity("天津");
        bar.setDesc("子对象描述1");
        bar.setId(1010L);
        bars.add(bar);
        base.setSubList(bars);

        final MainObject working = new MainObject();
        working.setName("新名称");
        List<Bar> barsWorking = new ArrayList<Bar>(1);
        Bar barWorking = new Bar();
        barWorking.setId(1010L);
        barWorking.setCity("天津");
        barWorking.setDesc("子对象描述2");
        barsWorking.add(barWorking);
        working.setSubList(barsWorking);

        DiffNode diff =
            ObjectDifferBuilder.startBuilding()
                               .identity().setDefaultCollectionItemIdentityStrategy(new IdentityStrategy() {
                @Override
                public boolean equals(Object working, Object base) {
                    if (working instanceof Bar && base instanceof Bar) {
                        System.out.println("自定义相等比较11111111111*****************,working:"+working+",base: "+base);
                        return ((Bar) working).getId().equals(((Bar) base).getId());
                    }
                    return false;
                }
            }).and().build().compare(working, base);

        System.out.println(diff);
        System.out.println("-------------------------------------------1");
        diff.visit(new DiffNode.Visitor() {

            @Override
            public void node(DiffNode node, Visit visit) {
                System.out.println(node.getPath() + " => " + node.getState());
            }
        });


        System.out.println("-------------------------------------------2");
        diff.visit(new DiffNode.Visitor() {
            public void node(DiffNode node, Visit visit) {
                final Object baseValue = node.canonicalGet(base);
                final Object workingValue = node.canonicalGet(working);
                final String message =
                    node.getPath() + " changed from " + baseValue + " to " + workingValue;
                System.out.println(message);
            }
        });

        System.out.println("-------------------------------------------3");
        System.out.println(SimpleDiff.diff(base, working, new SimpleDiffPathFormatter()));
    }
}
